<?php
/**
 * Admin Groups Controller
 *
 * This handles all AJAX requests from the Admin GUI Group page.
 * These methods will create an AJAX response containing JSON data. The JSON
 * data is read by the JS code and rendered into interface.
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_Admin_Groups_NewController extends RM_Controller
{
    function insertJsonAction(){
        $data = $this->_getParam('new_group');

        $languageModel = new RM_Languages();
        $languages = $languageModel->fetchAll();
        foreach ($languages as $language) {
            $data[$language->iso] = $data['name'];
        }
        unset($data['name']);
        
        $model = new RM_Groups();
        $key = $model->insert($data);

        $unitModel = new RM_Units();
        $unit = $unitModel->find($data['main_unit_id'])->current();
        if ($unit !== null) {
            $unit->group_id = $key;
        }
        $unit->save();

        return array(
            'data' => array('success' => true)
        );
    }
}